package com.ruoyi.zq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.zq.domin.ZqVolunteerActivity;
import com.ruoyi.common.zq.domin.vo.VolunteerActivityVO;
import com.ruoyi.zq.service.VolunteerActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ObjectUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 *
 * @Author: LoneyFeather
 * @Date: Created in 16:08 2021/12/24
 */
@Api(tags = "志愿服务活动管理")
@RestController
@RequestMapping("/volunteer")
public class VolunteerActivityController extends BaseController {


    @Autowired
    private VolunteerActivityService activityService;

    @ApiOperation("获取活动了列表")
    @PreAuthorize("@ss.hasPermi('zq:volunteer:query')")
    @GetMapping("/list")
    public TableDataInfo activityList(VolunteerActivityVO activityDto){

        startPage();
        List<VolunteerActivityVO> list = activityService.activityList(activityDto);
        return getDataTable(list);
    }

    @ApiOperation("获取活动详细信息")
    @PreAuthorize("@ss.hasPermi('zq:volunteer:query')")
    @GetMapping("/get/{activityId}")
    public AjaxResult getInfo(@ApiParam("活动id") @PathVariable(value = "activityId") Long activityId){
        AjaxResult ajax = AjaxResult.success();
        VolunteerActivityVO activityVO = activityService.getInforByActivityId(activityId);
        if (Objects.isNull(activityVO)){
            return ajax.put(AjaxResult.MSG_TAG,activityId+"：该条活动不存在或已被删除！！！");
        }else {
            ajax.put(AjaxResult.DATA_TAG,activityVO);
        }

        return ajax;
    }

    @ApiOperation("添加活动")
    @PreAuthorize("@ss.hasPermi('zq:volunteer:add')")
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@Validated @RequestBody ZqVolunteerActivity activityDTO) throws TaskException, SchedulerException {
        if (activityDTO.getActivityId() != null){
            ZqVolunteerActivity ob = activityService.getOne(new LambdaQueryWrapper<ZqVolunteerActivity>()
                    .eq(ZqVolunteerActivity::getActivityId,activityDTO.getActivityId())
            .eq(ZqVolunteerActivity::getIsDel,0));
            if(!Objects.isNull(ob)){
                return AjaxResult.error("活动id："+activityDTO.getActivityId() + "已存在");
            }
        }
        activityDTO.setCreateUserId(getUserId());
        activityDTO.setCreateBy(SecurityUtils.getUsername());
        activityDTO.setCreateTime(new Date());
        activityDTO.setUpdateTime(new Date());
        return toAjax(activityService.addActivity(activityDTO));
    }

    @ApiOperation("修改活动内容（包含手动打开和关闭按钮）")
    @PreAuthorize("@ss.hasPermi('zq:volunteer:edit')")
    @PostMapping("/update")
    public AjaxResult update(@Validated @RequestBody ZqVolunteerActivity activity){
        ZqVolunteerActivity volunteerActivity = activityService.getById(activity.getActivityId());
        if(getDeptId()!=100 && !getUserId().equals(volunteerActivity.getCreateUserId())){
            return AjaxResult.error("用户没有修改该活动的权限");
        }
        if(volunteerActivity.getApplyState()==2){
            return AjaxResult.error("该活动已经结束了,无法修改");
        }

        BeanUtils.copyProperties(activity,volunteerActivity);
        volunteerActivity.setUpdateBy(getUsername());
        volunteerActivity.setUpdateTime(new Date());
        return toAjax(activityService.updateAtivity(volunteerActivity));
    }

    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('zq:volunteer:remove')")
    @PostMapping("/remove/{activityIds}")
    public AjaxResult remove(@ApiParam("活动id") @PathVariable(value = "activityIds") Long[] activityIds){
        if (ObjectUtils.isEmpty(activityIds)){
            return AjaxResult.error("传入的参数为空！！！");
        }
        //逻辑删除
        return toAjax(activityService.removeActivity(activityIds));
    }

}
